<template>
  <div id="main">
    <el-row>
      <el-col>
        <div id="content">
          <h1 class="title">当前操作：修改密码</h1>
          <el-form ref="form" :rules="rules" :model="form" class="form">
            <el-form-item label="请输入当前密码" label-width="140px" prop="password">
              <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="请输入新密码" label-width="140px" prop="newPassword1">
              <el-input v-model="form.newPassword1"></el-input>
            </el-form-item>
            <el-form-item label="请再次输入新密码" label-width="140px" prop="newPassword2">
              <el-input v-model="form.newPassword2"></el-input>
            </el-form-item>
            <el-form-item class="button">
              <el-button type="primary" @click="onSubmit('form')">修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {post} from "../../common/request";
import jwt_decode from "_jwt-decode@3.1.2@jwt-decode";

export default {
  name: "UpdatePassword",
  data() {
    return {
      form: {
        password: "",
        newPassword1: "",
        newPassword2: ""
      },
      rules: {
        password: [
          {required: true, message: '请输入旧密码', trigger: 'blur'},
          {
            pattern: /^\d{6}$/,
            message: "请正确填写6位密码",
            trigger: "blur"
          }
        ],
        newPassword1: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {
            pattern: /^\d{6}$/,
            message: "请正确填写6位密码",
            trigger: "blur"
          }
        ],
        newPassword2: [
          {required: true, message: '请再次输入新密码', trigger: 'blur'},
          {
            pattern: /^\d{6}$/,
            message: "请正确填写6位密码",
            trigger: "blur"
          }
        ]
      }
    }
  },
  methods: {
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.form.newPassword1 !== this.form.newPassword2) {
            this.$message.warning("两次输入的密码不一致");
            return false;
          }

          post("/account/updatePassword", this.form).then(x => {
            if (x.code === 1) {
              this.$store.dispatch('asyncUpdateUser', '');
              this.$store.dispatch('asyncUpdateToken', '');
              //清除浏览器本地存储空间
              sessionStorage.removeItem('state');
              this.$message.success(x.msg);
              this.$router.push("/");
            } else {
              this.$message.error(x.msg);
            }
          });
        } else {
          this.$message.error("您的输入不合法")
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
#main {
  background-color: rgb(240, 240, 240);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

#content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.form {
  margin-top: 30px;
}

.button {
  text-align: center;
}
</style>
